/**
 * BibSonomy CV Wiki - Wiki for user and group CVs
 *
 * Copyright (C) 2006 - 2016 Knowledge & Data Engineering Group,
 *                               University of Kassel, Germany
 *                               http://www.kde.cs.uni-kassel.de/
 *                           Data Mining and Information Retrieval Group,
 *                               University of Würzburg, Germany
 *                               http://www.is.informatik.uni-wuerzburg.de/en/dmir/
 *                           L3S Research Center,
 *                               Leibniz University Hannover, Germany
 *                               http://www.l3s.de/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.bibsonomy.wiki.tags;

import static org.bibsonomy.util.ValidationUtils.present;

import org.bibsonomy.common.enums.GroupingEntity;

import info.bliki.htmlcleaner.Utils;

/**
 * Used for rending tags which are available for both users and groups, i.e. shared tags.
 * 
 * @author Bernd Terbrack
 */
public abstract class SharedTag extends AbstractTag {
	
	private static final String EMPTY_NAME = "";

	/**
	 * Create new shared tag.
	 * 
	 * @param name the name of the tag
	 */
	public SharedTag(final String name) {
		super(name);
	}

	@Override
	protected String renderSafe() {
		return this.renderSharedTag();
	}
	
	/**
	 * return the currently requested grouping entity - i.e. 
	 *   GroupingEntity.USER if we're working on a user, and
	 *   GroupingEntity.GROUP when working on a group.
	 *   
	 * @return - the corresponding grouping entity.
	 */
	protected GroupingEntity getGroupingEntity() {
		// If the group is "null" then its a user (obviously)
		return this.requestedGroup != null ? GroupingEntity.GROUP : GroupingEntity.USER;
	}
	
	/**
	 * Return the realname of the requested user / group.
	 * 
	 * If the requesting user isn't allowed to see the real name of the requested
	 * user / group, we return the ordinary username instead.
	 * @return the realname
	 */
	protected String getRequestedRealName() {
		switch (getGroupingEntity()) {
		case USER:
			return Utils.escapeXmlChars( present(this.requestedUser.getRealname()) ? this.requestedUser.getRealname() : this.requestedUser.getName() );
		case GROUP:
			return Utils.escapeXmlChars( present(this.requestedGroup.getRealname()) ? this.requestedGroup.getRealname() : this.requestedGroup.getName() );
		default: 
			return EMPTY_NAME;
		}
	}
	
	/**
	 * Return the name of the requested user / group.
	 * @return The name of the User/Group based on the requestType
	 */
	protected String getRequestedName() {
		switch (getGroupingEntity()) {
		case USER:
			return Utils.escapeXmlChars(this.requestedUser.getName());
		case GROUP:
			return Utils.escapeXmlChars(this.requestedGroup.getName());
		default: 
			return EMPTY_NAME;
		}
	}

	/**
	 * internal render method
	 * @return the string to append to the output
	 */
	protected abstract String renderSharedTag();

}
